package com.topfinance.xml.handler.ibps;


import java.io.StringWriter;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.Marshaller;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.topfinance.xml.IMsgHandler;
import com.topfinance.xml.MsgHeader;

import com.cnaps2.xml.iso20022.pacs.v00800102.*;

public class Msg101Handler implements IMsgHandler {

	public static final String PACKAGE = "com.cnaps2.xml.pacs.v00800102";
	protected static Log log = LogFactory.getLog(Msg101Handler.class);
	
	public Msg101Handler() {
		super();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.ecados.svc.cnaps2.xml.IMsgHandler#doInFlowMsg(com.ecados.xml.v00100101
	 * .Document)
	 */
	public void doInFlowMsg(String head,Object docObj) throws Exception {
		Document docRef = (Document) docObj;
		try {
			MsgHeader msgHead = MsgHeader.getInstance();
			msgHead.parse(head);
			System.out.println("[MesgID]" + msgHead.getMesgID());
			System.out.println("[" + PACKAGE + "]"+ docRef.getFiToFICstmrCdtTrf().getGrpHdr().getMsgId());
			// do business logic
		} catch (Exception e) {
			e.printStackTrace();
			// log.error("Parsing xml to Object failed.",e);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.ecados.test.IMsgHandler#doOutFlowMsg(java.util.Map)
	 */
	public String doOutFlowMsg(Map dataMap) throws Exception {
		String xmlStr = "";
		try {
//			//System.out.println("in genOutFlowMsg.");
			// gen Document from dataMap
			Document document = new Document();
			JAXBContext jaxbContext = JAXBContext.newInstance(PACKAGE);
			Marshaller marshaller = jaxbContext.createMarshaller();
			// marshaller.setProperty(Marshaller.JAXB_ENCODING,Constants.ENCODING_GBK);
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
			//XMLGregorianCalendar  calendar = (XMLGregorianCalendar) new Cloneable() ;
			
			ObjectFactory factory = new ObjectFactory();
			GroupHeader33 grpHeader = factory.createGroupHeader33();		
			grpHeader.setMsgId("123456789012");
			grpHeader.setBtchBookg(false);
			grpHeader.setCtrlSum(new BigDecimal(1));
			//grpHeader.setIntrBkSttlmDt(XMLGregorianCalendar. new Date());
			
			SettlementInformation13 sttlmInf = factory.createSettlementInformation13();
			sttlmInf.setSttlmMtd(SettlementMethod1Code.CLRG);
			
			ActiveCurrencyAndAmount ttlIntrBkSttlmAmt = factory.createActiveCurrencyAndAmount();
			ttlIntrBkSttlmAmt.setValue(new BigDecimal(100));
			ttlIntrBkSttlmAmt.setCcy("RMB");
			
			grpHeader.setTtlIntrBkSttlmAmt(ttlIntrBkSttlmAmt);
			grpHeader.setSttlmInf(sttlmInf);
			
			List<CreditTransferTransactionInformation11> list = new ArrayList();
			//grpHeader.set
			//[66]00100
			//[20]0606
			//[64]098765432109
			//[63]收款人名称
			//[62]1234567890098765
			//[16]00100
			//[15]123456789012
			//[60]098765432109
			//[14]1234567890123456789012345678
			//[11]附言
			//[7]付款人名称
			//[6]123456789012
			//[5]000000000000100.01
			//[4]098765432109
			//[3]098765432109
			//[2]123456789012
			
			FIToFICustomerCreditTransferV02 root = factory.createFIToFICustomerCreditTransferV02();
			root.setGrpHdr(grpHeader);
		
			document.setFiToFICstmrCdtTrf(root);
			JAXBElement<Document> webFlowElement = factory.createDocument(document);
			
			StringWriter sw = new StringWriter();
			marshaller.marshal(webFlowElement, sw);
			xmlStr = sw.toString();
			// do business logic
		} catch (Exception e) {
			log.error(" failed.",e);
		}
		return xmlStr;
	}
}